package com.sandu.user.fragment

import android.os.Bundle
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.ToastUtils
import com.sandu.lib_common.base.BaseDBFragment
import com.sandu.lib_common.base.appViewModel
import com.sandu.lib_common.ext.init
import com.sandu.lib_common.ext.joinQQGroup
import com.sandu.lib_common.ext.jumpByLogin
import com.sandu.lib_common.ext.setUiTheme
import com.sandu.lib_common.utils.JumpUtils
import com.sandu.lib_mvvm.ext.nav
import com.sandu.lib_mvvm.ext.navigateAction
import com.sandu.lib_mvvm.ext.parseState
import com.sandu.lib_mvvm.ext.util.notNull
import com.sandu.lib_project_network.app.network.ApiService
import com.sandu.lib_project_network.app.network.NetworkApi
import com.sandu.lib_project_network.bean.IntegralResponse
import com.sandu.user.ScrollingActivity
import com.sandu.user.activity.LoginActivity
import com.sandu.user.activity.MineActivity
import com.sandu.user.databinding.MineFragmentBinding
import com.sandu.user.viewmodel.request.RequestMineViewModel
import com.sandu.user.viewmodel.state.MineViewModel

class MineFragment(private var mainViewpager: ViewPager2) :
    BaseDBFragment<MineViewModel, MineFragmentBinding>() {
    private var rank: IntegralResponse? = null

    private val requestMeViewModel: RequestMineViewModel by viewModels()
    override fun initView(savedInstanceState: Bundle?) {
//        mViewBind.mineText.setOnClickListener { mainViewpager.setCurrentItem(2, false) }
        mDatabind.mineText.setOnClickListener {
            var params = Pair("test", "this is params value")
            activity?.let { it1 -> JumpUtils.jumpTo(it1, MineActivity::class.java, params) }
        }
        //数据绑定
        mDatabind.vm = mViewModel
        mDatabind.click = ProxyClick()
        mDatabind.meSwipe.init {
            requestMeViewModel.getIntegral()
        }

        appViewModel.appColor.value?.let {
            setUiTheme(
                it,
                mDatabind.meLinear,
                mDatabind.meIntegral
            )
        }
        appViewModel.userInfo.value?.let { mViewModel.name.set(if (it.nickname.isEmpty()) it.username else it.nickname) }
    }


    override fun lazyLoadData() {
        appViewModel.userInfo.value?.run {
            mDatabind.meSwipe.isRefreshing = true
            requestMeViewModel.getIntegral()
        }
    }

    override fun createObserver() {

        requestMeViewModel.meData.observe(viewLifecycleOwner, Observer { resultState ->
            mDatabind.meSwipe.isRefreshing = false
            parseState(resultState, {
                rank = it
                mViewModel.info.set("id：${it.userId}　排名：${it.rank}")
                mViewModel.integral.set(it.coinCount)
            }, {
                ToastUtils.showShort(it.errorMsg)
            })
        })

        appViewModel.run {
            appColor.observeInFragment(this@MineFragment, Observer {
                setUiTheme(it, mDatabind.meLinear, mDatabind.meSwipe, mDatabind.meIntegral)
            })
            userInfo.observeInFragment(this@MineFragment) {
                it.notNull({
                    mDatabind.meSwipe.isRefreshing = true
                    mViewModel.name.set(if (it.nickname.isEmpty()) it.username else it.nickname)
                    requestMeViewModel.getIntegral()
                }, {
                    mViewModel.name.set("请先登录~")
                    mViewModel.info.set("id：--　排名：--")
                    mViewModel.integral.set(0)
                })
            }
        }
    }

    inner class ProxyClick {

        /** 登录 */
        fun login() {
            activity?.let { it -> JumpUtils.jumpTo(it, LoginActivity::class.java) }
        }

        /** 收藏 */
        fun collect() {

            ApiService.SERVER_URL =
                if (ApiService.SERVER_URL.contains("https")) "http://wanandroid.com/" else "https://wanandroid.com/"
            NetworkApi.INSTANCE.resetApi()
//            activity?.let { it1 -> JumpUtils.jumpTo(it1, MineActivity::class.java) }
        }

        /** 积分 */
        fun integral() {
            var test: MutableList<String> = arrayListOf()
            test.removeAt(1)

            activity?.let { it1 -> JumpUtils.jumpTo(it1, MineActivity::class.java) }
        }

        /** 文章 */
        fun ariticle() {
            activity?.let { it1 -> JumpUtils.jumpToByLogin(it1, ScrollingActivity::class.java) }
        }

        fun todo() {
            activity?.let { it1 -> JumpUtils.jumpTo(it1, MineActivity::class.java) }
        }

        /** 玩Android开源网站 */
        fun about() {
            activity?.let { it1 -> JumpUtils.jumpTo(it1, MineActivity::class.java) }
        }

        /** 加入我们 */
        fun join() {
            joinQQGroup("9n4i5sHt4189d4DvbotKiCHy-5jZtD4D")
        }

        /** 设置*/
        fun setting() {
            activity?.let { it1 -> JumpUtils.jumpTo(it1, MineActivity::class.java) }
        }

    }

}